{include file="header.html"}
<body class="gr">

<div class="navBar">
	<div class="titleView">订单</div>
</div>

<div class="main-top main-bottom order-index">
	<div class="top">
		<ul>
			<li>
				<a href="wap.php?app=order&act=index" {if !isset($smarty.get.status)}class="this"{/if}><span>全部</span></a>
			</li>
			<li>
				<a href="wap.php?app=order&act=index&status=0" {if $status=='0'}class="this"{/if}><span>未支付</span></a>
			</li>
			<li>
				<a href="wap.php?app=order&act=index&status=3" {if $status=='3'}class="this"{/if}><span>进行中</span></a>
			</li>
			<li>
				<a href="wap.php?app=order&act=index&status=4" {if $status=='4'}class="this"{/if}><span>待评价</span></a>
			</li>
		</ul>
	</div>
	
	<div class="list pullRefresh">
		{if count($data)>0}
		<ul>
			{foreach from=$data name=g item=g}
			<li>
				<div class="view">
					<a href="wap.php?app=order&act=detail&id={$g->id}">
						<div class="statusView ge-bottom">
							<span class="status{$g->status}">{$g->status_name}</span>
							{if $g->shop_id>0}
							<i class="shop"></i>{$g->shop_name}
							{else if $g->beautician_id>0}
							<i class="master"></i>{$g->beautician_name}
							{/if}
						</div>
						<div class="projectView ge-bottom">
							{foreach from=$g->projects name=p item=p}
							<div class="project">
								<div class="pic" style="background-image:url({$p->project_pic});"></div>
								{$p->project_name}
							</div>
							{/foreach}
						</div>
						<div class="infoView ge-bottom">
							{if $g->shop_id>0 && $g->beautician_id>0}<div><span>指定美容师</span>{$g->beautician_name}</div>{/if}
							<div><span>预约时间</span>{$g->service_time}</div>
							<div><span>服务地址</span>{$g->full_address}</div>
						</div>
					</a>
					<div class="btnView">
						{if $g->status==0}<a class="pay" href="javascript:void(0)">去付款</a>{/if}
						{if $g->status==1 && $g->can_edit_time==1}<a class="edit" href="javascript:void(0)" mid="{$g->id}" shop_id="{$g->shop_id}" beautician_id="{$g->beautician_id}" project_ids="{trim($g->project_ids,',')}" service_time="{$g->service_time}">修改时间</a>{/if}
						{if $g->status==4}<a class="green comment" href="javascript:void(0)" mid="{$g->id}">去评价</a><!--<a href="javascript:void(0)">再次预约</a>-->{/if}
						{if $g->status==5}<!--<a href="javascript:void(0)">再次预约</a>-->{/if}
						实付款 ￥{$g->total_price}
						{if $g->status==0}
						<form action="wap.php?app=order&act=order_pay" method="post">
						<input type="hidden" name="order_sn" value="{$g->order_sn}" />
						<input type="hidden" name="price" value="{$g->total_price}" />
						<input type="hidden" name="body" value="i美-项目订单" />
						<input type="hidden" name="pay_method_name" value="微信支付" />
						</form>
						{/if}
					</div>
				</div>
			</li>
			{/foreach}
		</ul>
		{else}
		<div class="no-record">当前没有任何记录</div>
		{/if}
	</div>
</div>

<div class="footer">
	<a class="ico1" href="/wap.php"></a>
	<a class="ico2 this" href="/wap.php?app=order&act=index"></a>
	<a class="ico3" href="/wap.php?app=member&act=index"></a>
</div>

{include file="footer.html"}
<script>
var offset = $('.list li').length, service_time_g = [];
function createHtml(g){
	var html = '<li>\
					<div class="view">\
						<a href="wap.php?app=order&act=detail&id='+g.id+'">\
							<div class="statusView ge-bottom">\
								<span class="status'+g.status+'">'+g.status_name+'</span>';
								if(g.shop_id>0){
								html += '<i class="shop"></i>'+g.shop_name;
								}else if(g.beautician_id>0){
								html += '<i class="master"></i>'+(g.beautician_name!=null?g.beautician_name:'');
								}
							html += '</div>\
							<div class="projectView ge-bottom">';
								if($.isArray(g.projects)){
								for(var p=0; p<g.projects.length; p++){
								html += '<div class="project">\
									<div class="pic" style="background-image:url('+g.projects[p].project_pic+');"></div>\
									'+g.projects[p].project_name+'\
								</div>';
								}
								}
							html += '</div>\
							<div class="infoView ge-bottom">';
								if(g.shop_id>0 && g.beautician_id>0)html += '<div><span>指定美容师</span>'+g.beautician_name+'</div>';
								html += '<div><span>预约时间</span>'+g.service_time+'</div>\
								<div><span>服务地址</span>'+g.full_address+'</div>\
							</div>\
						</a>\
						<div class="btnView">';
							if(g.status==0)html += '<a class="pay" href="javascript:void(0)">去付款</a>';
							if(g.status==1 && g.can_edit_time==1)html += '<a class="edit" href="javascript:void(0)" mid="'+g.id+'" shop_id="'+g.shop_id+'" beautician_id="'+g.beautician_id+'" project_ids="'+g.project_ids.trim(',')+'" service_time="'+g.service_time+'">修改时间</a>';
							if(g.status==4)html += '<a class="green" href="javascript:void(0)" mid="'+g.id+'">去评价</a>';//<a href="javascript:void(0)">再次预约</a>
							//if(g.status==5)html += '<a href="javascript:void(0)">再次预约</a>';
							html += '实付款 ￥'+g.total_price;
							if(g.status==0){
								html += '<form action="wap.php?app=order&act=order_pay" method="post">\
								<input type="hidden" name="order_sn" value="'+g.order_sn+'" />\
								<input type="hidden" name="price" value="'+g.total_price+'" />\
								<input type="hidden" name="body" value="i美-项目订单" />\
								<input type="hidden" name="pay_method_name" value="微信支付" />\
								</form>';
							}
						html += '</div>\
					</div>\
				</li>';
	offset++;
	return html;
}
function checkBind(){
	$('.btnView a.pay').each(function(){
		if(!!$(this).data('bind'))return true;
		$(this).data('bind', true).click(function(){
			$(this).parent().find('form').submit();
		});
	});
	$('.btnView a.comment').each(function(){
		if(!!$(this).data('bind'))return true;
		$(this).data('bind', true).click(function(){
			location.href = 'wap.php?tpl=comment.index&id='+$(this).attr('mid')+'&url='+location.href.urlencode();
		});
	});
	$('.btnView a.edit').each(function(){
		if(!!$(this).data('bind'))return true;
		$(this).data('bind', true).click(function(){
			if(!confirm('每个订单只有一次修改时间的机会，您确认要修改么？'))return false;
			getServiceTime($(this), function(){
				var g = service_time_g;
				$('.faceView .dates a').click(function(){
					$('.faceView .dates a').removeClass('x');
					$(this).addClass('x');
					var html = '', r = g[$(this).index()].schedule;
					for(var i=0; i<r.length; i++){
						html += '<a href="javascript:void(0)" timestamp="'+r[i].timestamp+'" '+(r[i].available!=0?'class="x"':'')+'>\
							<div class="view"><span>'+r[i].hour+'</span></div>\
						</a>';
					}
					$('.faceView .dateView').html(html);
					$('.faceView .dateView a.x').click(function(){
						setServiceTime.call($(this));
					});
				});
				$('.faceView .dateView a.x').click(function(){
					setServiceTime.call($(this));
				});
				$('.faceView .buttonView .btn').click(function(){
					var mid = $(this).attr('mid'), service_time = $(this).attr('service_time');
					if(!!!service_time){
						$.overloadError('请选择服务时间');
						return;
					}
					$.overload(null);
					$.postJSON('api.php?app=order&act=edit_time', { id:mid, service_time:service_time }, function(json){
						if(json.error!=0){
							$.overloadError(json.msg);
							return;
						}
						$.overloadSuccess('修改成功');
						removeFaceView();
					});
				});
			});
		});
	});
}
function resize(){
	$('.list').height($.window().height-44-$('.top').height()-$('.footer').height());
}
$(window).resize(resize);
$(function(){
	resize();
	checkBind();
	$('.pullRefresh').pullRefresh({
		header : true,
		footer : true,
		refresh : function(fn){
			var _this = this;
			offset = 0;
			$.getJSON('api.php?app=order&act=index&status={$status}', function(json){
				if(json.error==1){
					alert(json.msg);
					return;
				}
				if($.isArray(json.data)){
					var html = '';
					for(var i=0; i<json.data.length; i++)html += createHtml(json.data[i]);
					if(html.length)_this.find('ul').html(html);
					checkBind();
				}
				fn();
			});
		},
		load : function(fn){
			var _this = this;
			$.getJSON('api.php?app=order&act=index&status={$status}', { offset:offset }, function(json){
				if(json.error==1){
					alert(json.msg);
					return;
				}
				if($.isArray(json.data)){
					var html = '';
					for(var i=0; i<json.data.length; i++)html += createHtml(json.data[i]);
					_this.find('ul').append(html);
					checkBind();
				}
				fn();
			});
		}
	});
});

function bindFaceView(faceView){
	var titleView = $('.faceView .navBar .titleView');
	titleView.css('margin-left', -titleView.width()/2);
	$('.faceView .navBar .left').click(function(){
		removeFaceView(faceView);
	});
	$('.faceView').each(function(){
		var height = $.window().height;
		if($(this).outerHeight(true)<height)$(this).height(height);
	});
}
function removeFaceView(faceView){
	if(typeof faceView == 'undefined')faceView = '.faceView';
	$(faceView).removeClass('x');
	setTimeout(function(){ $(faceView).remove() }, 400);
}

function setServiceTime(){
	$('.faceView .dateView a').removeClass('this');
	$(this).addClass('this');
	var timestamp = $(this).attr('timestamp').toDate('yyyy-mm-dd')+' '+$.trim($(this).text());
	$('.faceView .timeView span').html(timestamp);
	$('.faceView .buttonView .btn').attr('service_time', timestamp);
}

function getServiceTime(_this, fn){
	var type_id = (!isNaN(_this.attr('shop_id')) && parseInt(_this.attr('shop_id'))>0) ? 2 : 1;
	$.overload(null);
	$.getJSON('api.php?app=home&act=schedule&shop_id='+_this.attr('shop_id')+'&beautician_id='+_this.attr('beautician_id')+'&project_ids='+_this.attr('project_ids')+'&type_id='+type_id, function(json){
		$.overload(false);
		if(json.error==1){
			alert(json.msg);
			return;
		}
		var g = json.data;
		if(!$.isArray(g)){
			alert('缺少数据');
			return;
		}
		service_time_g = g;
		var html = '<div class="faceView gr">\
						<div class="navBar">\
							<a class="left" href="javascript:void(0)"><i class="return"></i></a>\
							<div class="titleView">服务时间</div>\
						</div>\
						<div class="main-top home-schedule">\
							<div class="dates">';
								for(var i=0; i<g.length; i++){
									html += '<a href="javascript:void(0)" '+(!i?'class="x"':'')+'>\
										<div class="view">\
											<span>'+g[i].name+'</span>\
											<div>'+g[i].date+'</div>\
										</div>\
									</a>';
								}
							html += '</div>\
							<div class="dateView">';
								var r = g[0].schedule;
								for(var i=0; i<r.length; i++){
									html += '<a href="javascript:void(0)" timestamp="'+r[i].timestamp+'" '+(r[i].available!=0?'class="x"':'')+'>\
										<div class="view"><span>'+r[i].hour+'</span></div>\
									</a>';
								}
							html += '<div class="clear"></div></div>\
							<div class="timeView ge-top ge-bottom"><div>服务时间</div><span>'+_this.attr('service_time')+'</span></div>\
							<div class="buttonView"><a href="javascript:void(0)" class="btn pass" mid="'+_this.attr('mid')+'">确认修改</a></div>\
						</div>\
					</div>';
		$('body').append(html);
		setTimeout(function(){ $('.faceView').addClass('x') }, 10);
		bindFaceView();
		fn(g);
	});
}
</script>